var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');

var app = getApp();

Page({
  data: {
    voucherList: [],
    page: 1,
    size: 6,
    totalPages: 1,
    showType: 1,
    type: 0,
    name: ''
  },
  getVoucherList() {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    util.request(api.VoucherList, { showType: that.data.showType, name: that.data.name.replace(/\s*/g, ""), page: that.data.page, size: that.data.size }).then(function (res) {
      if (res.errno === 0) {        
        that.setData({
          voucherList: that.data.voucherList.concat(res.data.voucherList),         
          totalPages: res.data.totalPages,
          showType: res.data.showType         
        });
      }
      wx.hideLoading();
    });
  },
  onLoad: function (options) {
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getVoucherList();     
    } else {
      wx.showToast({
        title: '没有更多代金券了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
  switchTab: function (event) {
    let showType = event.currentTarget.dataset.index;
    this.setData({
      showType: showType
    });
    this.setData({
      page: 1
    });
    this.setData({
      voucherList: []
    })
    this.getVoucherList();
  },
  onReady: function () {
  },
  onShow: function () {
    if (isNaN(this.data.showType)) {
      this.setData({
        showType: 1
      })
    }
    this.setData({
      page: 1
    });
    this.setData({
      voucherList: []
    })
    this.getVoucherList();
  },
  inputChange: function (e) {
    this.setData({
      name: e.detail.value,
    });
  },
  onKeywordConfirm(event) {
    this.setData({
      page: 1
    });
    this.setData({
      voucherList: []
    })
    this.getVoucherList()
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      page: 1
    });
    this.setData({
      voucherList: []
    })
    this.getVoucherList()
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})